<?php

namespace System\Database;

if (!defined('BASEPATH')) die('Access Denied.');

/**
 * 
 * µMVC Framework (c) 2011.
 * 
 * PHP5 MVC Framework software for PHP 5.2.17 or later
 * 
 * Database adapted from Obullo framework based on work from CodeIgniter
 * 
 * @package         µMVC
 * @filename        system/database/dbfactory.class.php
 * @copyright       Lanz (c) 2011.
 * 
 * @author          Ersin Güvenç
 * 
 */

use \PDO as PDO;
use \PDOStatement as PDOStatement;

/**
 * DBFactory class
 * 
 * @package         µMVC
 * @subpackage      Database
 * @category        System
 */
class DBFactoryException extends \System\Core\CommonException {}

Class DBFactory
{

    private $active_group = 'db';

    /**
     * Include database file
     * and build database params
     * 
     * @author   Ersin Güvenç
     * @return   void
     */
    public function __construct($active_db = '')
    {
        if ($active_db != '')
        {
            $this->active_group = $active_db;
        }
    }

    /**
     * Connect Requested PDO Drivers
     * 
     * @author   Ersin Güvenç
     * @param    
     * @version  0.1
     * @version  0.2 added $active_group and $settings vars
     * @version  0.3 added $param var, set db paramaters manually for
     *           dynamic connections
     * @return   object PDO Object
     */
    public function connect($param = '')
    {
        if (is_array($param))
        {
            $hostname = $param['hostname'];
            $username = $param['username'];
            $password = $param['password'];
            $database = $param['database'];
            $dbdriver = $param['dbdriver'];
            $char_set = $param['char_set'];
            $dbh_port = $param['dbh_port'];
        }
        else
        {      
            $config = \System\Core\Config::instance();
            $hostname = $config->getItem('hostname');
            $username = $config->getItem('username');
            $password = $config->getItem('password');
            $database = $config->getItem('database');
            $dbdriver = $config->getItem('dbdriver');
            $char_set = $config->getItem('char_set');
            $dbh_port = $config->getItem('dbh_port');
        }

        if (empty($dbdriver))
        {
            throw new DBFactoryException('Please set a valid DB driver from config/database file!');
        }

        $dbdriver = strtoupper($dbdriver);

        switch ($dbdriver)
        {
            // FreeTDS / Microsoft SQL Server / Sybase
            case 'DBLIB':
                $dbh = new DB("dblib:host=$hostname:$dbh_port;dbname=$database;charset=$char_set", "$username", "$password");
                break;

            // Firebird/Interbase 6   
            case 'FIREBIRD':
                $dbh = new DB("firebird:dbname=$database", "$username", "$password");
                break;

            // IBM DB2  
            case 'IBM':
                $dbh = new DB("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" . "HOSTNAME=$hostname;PORT=$dbh_port;PROTOCOL=TCPIP;", "$username", "$password");
                break;

            // IBM Informix Dynamic Server  
            case 'INFORMIX':
                $dbh = new DB("informix:host=$hostname; service=9800;
             database=$database; server=ids_server; protocol=onsoctcp;EnableScrollableCursors=1", "$username", "$password");
                break;

            // MySQL 3.x/4.x/5.x  
            case 'MYSQL':
                // array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $char_set") it occurs an error !
                $dbh = new DB("mysql:host=$hostname;dbname=$database", "$username", "$password");
                $dbh->query("SET NAMES $char_set");
                break;

            // Oracle Call Interface  
            case 'OCI':
                $dbh = new DB("oci:dbname=$hostname/$database;charset=$char_set", "$username", "$password");
                break;

            // ODBC v3 (IBM DB2, unixODBC and win32 ODBC) 
            case 'ODBC':
                $dbh = new DB("odbc:Driver={SQL Native Client};Server=$hostname;Database=$database;Uid=$username;Pwd=$password;");
                break;

            // PostgreSQL  
            case 'PGSQL':
                $dbh = new DB("pgsql:dbname=$database;user=$username;password=$password;host=$hostname");
                break;

            // SQLite 3 and SQLite 2
            case 'SQLITE':
                $dbh = new DB("sqlite:$database");
                break;

            // 4D
            case '4D':
                $dbh = new DB("4D:host=$hostname;charset=$char_set", "$username", "$password");
                break;

            default:
                throw new DBFactoryException('This DB Driver does not support: ' . $dbdriver);
        } //end switch.
        // We set exception attribute for showing the pdo exceptions errors 
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $dbh->set_driver($dbdriver);

        return $dbh;
    }

}


?>
